<template>
  <div class="article-meta">
    <nuxt-link
      :to="{name:'profile',params:{
            username: article.author.username
        }
        }"
    >
      <img :src="article.author.image" />
    </nuxt-link>
    <div class="info">
      <nuxt-link
        :to="{name:'profile',
            params:{
                username: article.author.username
            }
        }"
        class="author"
      >{{ article.author.username }}</nuxt-link>
      <span class="date">{{ article.createdAt | date('MMM DD,YYYY') }}</span>
    </div>
    <template v-if="article.author.username !== user.username">
      <button
        class="btn btn-sm btn-outline-secondary"
        :class="{active: article.author.following}"
        :disabled="article.followDisabled"
        @click="onFollow"
      >
        <i class="ion-plus-round"></i>
        &nbsp;
        {{ article.author.following ? 'Unfaollow':'Follow' }} {{ article.author.username }}
      </button>
      &nbsp;&nbsp;
      <button
        class="btn btn-sm btn-outline-primary"
        :class="{active: article.favorited}"
        :disabled="article.favoriteDisabled"
        @click="onFavorite"
      >
        <i class="ion-heart"></i>
        &nbsp;
        {{ article.favorited ? 'Unfavorite':'Favorite' }} Post
        <span
          class="counter"
        >({{ article.favoritesCount }})</span>
      </button>
    </template>
    <template v-else>
      <nuxt-link
        class="btn btn-outline-secondary btn-sm"
        :to="{
          name:'edit',
          query:{
            slug: article.slug
          }
        }"
      >
        <i class="ion-edit"></i> Edit Article
      </nuxt-link>
      <button class="btn btn-outline-danger btn-sm" @click="deleteArt">
        <i class="ion-trash-a"></i> Delete Article
      </button>
    </template>
  </div>
</template>
<script>
import { mapState } from "vuex";
import {
  addFavorite,
  deleteFavorite,
  addFollow,
  deleteFollow,
  deleteArticles
} from "@/api/article";
export default {
  name: "ArticleMeta",
  props: {
    article: {
      type: Object,
      required: true,
    },
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
     async deleteArt (){
      await deleteArticles(this.article.slug)
      this.$router.push('/')

    },
    async onFavorite() {
      let article = this.article;
      article.favoriteDisabled = true;
      if (article.favorited) {
        //取消点赞
        await deleteFavorite(article.slug);
        article.favorited = false;
        article.favoritesCount += -1;
      } else {
        //添加点赞
        await addFavorite(article.slug);
        article.favorited = true;
        article.favoritesCount += 1;
      }
      article.favoriteDisabled = false;
    },
    async onFollow() {
      let article = this.article;
      article.followDisabled = true;
      if (article.author.following) {
        //取消点赞
        await deleteFollow(article.author.username);
        article.author.following = false;
      } else {
        //添加点赞
        await addFollow(article.author.username);
        article.author.following = true;
      }
      article.followDisabled = false;
    },
  },
};
</script>